package com.uyayo.oms.system.controller;

import com.uyayo.framework.common.annotation.MethodName;
import com.uyayo.framework.excelhelper.service.BaseExportExcelService;
import com.uyayo.framework.message.model.Result;
import com.uyayo.oms.system.biz.ParamBiz;
import com.uyayo.framework.pagehelper.pojo.PageBean;
import com.uyayo.framework.pagehelper.pojo.PageResult;
import com.uyayo.system.monitor.entity.SysException;
import com.uyayo.system.monitor.model.SysExceptionCondition;
import com.uyayo.system.monitor.service.ExceptionsService;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletResponse;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.OutputStream;
import java.util.List;

/**
 * 
 * 系统异常的Controller
 *
 * @author 乐乐科技.胡逢君
 * @date 2015-4-13下午4:03:35
 * @version 1.0
 */
@Controller
@RequestMapping("/system/monitor/exception")
public class SysExceptionMonitorController {

	@Autowired
	@Qualifier("baseExportExcelXSSFService")
	private BaseExportExcelService baseExportExcelService;

	@Autowired 
	private ExceptionsService exceptionsService;
	@Autowired
	private ParamBiz paramBiz;
	
	@RequestMapping(value="index",method=RequestMethod.GET)
	@MethodName(value="系统管理->系统监控->系统异常监控")
	public String index() throws Exception {
		return paramBiz.getManagerTemplatesPrefix()+"system/monitor/exception/index";

	}

	@ResponseBody
	@RequestMapping("grid")
	@MethodName(value="系统管理->系统监控->系统异常监控->查询列表",type=2)
	public  PageResult<SysException> grid(@RequestParam(value = "offset",required = false, defaultValue = "1") Integer start, //第多少行之后开始
                                          @RequestParam(value = "limit", required = false, defaultValue = "10") Integer pageSize, String sort, String order, SysExceptionCondition sysExceptionCondition) throws Exception{
		PageBean<SysException> pageBean=new PageBean<>();
		try {
			pageBean = exceptionsService.selectException((start / pageSize) + 1, pageSize,sort,order,sysExceptionCondition);
		} catch (Exception e) {
			e.printStackTrace();		
		}
		PageResult<SysException> pageResult = new PageResult<>();
		pageResult.setRows(pageBean.getList());
		pageResult.setTotal(pageBean.getTotalCount());
		return pageResult;
	}
	
	/**
	 * 查看
	 */
	@RequestMapping(value = "view", method = RequestMethod.GET)
	@MethodName(value="系统管理->监控管理->系统事件监控->查看",type=2)
	@RequiresPermissions("system:monitor:exception:view")
	public ModelAndView view(ModelAndView modelAndView,String id) {
		SysException sysException = new SysException();
		try {
			sysException = exceptionsService.getExceptionsById(id);
		} catch (Exception e) {
			e.printStackTrace();
		}
		modelAndView.addObject("sysException",sysException);
		modelAndView.setViewName(paramBiz.getManagerTemplatesPrefix()+"system/monitor/exception/view");
		return modelAndView;
	}
	
	/**
	 * 批量删除记录
	 * @throws Exception 
	 */
	@ResponseBody
	@RequestMapping("delete")
	@MethodName(value="系统管理->系统监控->系统异常监控->删除系统异常监控",type=2)
	public Result deleteException(String ids) throws Exception {
		Result customResult = exceptionsService.deleteException(ids);
		return customResult;
	}


	@RequestMapping(value = "export", method = RequestMethod.GET)
	@RequiresPermissions(value = "system:monitor:exception:export")
	@MethodName(value="系统管理->系统监控->系统异常监控->导出Excel",type=2)
	public ModelAndView exportExcel(HSSFWorkbook workbook, HttpServletResponse response,String sort,String order, SysExceptionCondition sysExceptionCondition) throws Exception {
		response.setContentType("octets/stream;charset=UTF-8");

		String fileName = new String("系统异常监控".getBytes("UTF-8"),"ISO8859-1");
		response.addHeader("Content-Disposition", "attachment;filename="+fileName+".xls");

		String[] headers = {"异常类 ","异常方法 ","事件详情","激活时间","备注"};
		String[] fieldNames = {"clazz","methodName","description","createdTime","remark"};

		List<SysException> sysExceptions;

		try {

			sysExceptions = exceptionsService.selectException(sort,order,sysExceptionCondition);

			OutputStream out = response.getOutputStream();
			this.baseExportExcelService.exportExcel(workbook,0,"系统异常监控表",headers,fieldNames, sysExceptions, "yyyy-MM-dd HH:mm:ss");
			workbook.write(out);
			out.close();

		} catch (FileNotFoundException e) {

			e.printStackTrace();

		} catch (IOException e) {

			e.printStackTrace();

		}
		return null;
	}
}
